import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { AuthService } from '../auth.service';
import { User } from '../../../database/schemas/user.schema';
import { Strategy } from 'passport-local';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super();
  }

  async validate(username: string, passport: string): Promise<User> {
    const user = this.authService.validateUser(username, passport);
    if (user) return user;
    throw new UnauthorizedException('incorrect username or password');
  }
}
